import 'package:fluent_ui/fluent_ui.dart';

///按钮工具类
class Buttons {
  Buttons._();

  static final Buttons _singleton = Buttons._();

  ///工厂单例
  factory Buttons() => _singleton;

  ///构建按钮
  Button build({
    final IconData? iconData,
    final String? label,
    final VoidCallback? onPressed,
  }) {
    final icon = Icon(iconData);
    if (label == null || label.isEmpty) {
      return Button(onPressed: onPressed, child: icon);
    }
    return Button(
      onPressed: onPressed,
      child: Builder(builder: (context) {
        return IconTheme(
          data: const IconThemeData(size: 20.0),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              icon,
              const SizedBox(width: 5.0),
              Text(label),
            ],
          ),
        );
      }),
    );
  }

  ///新增
  Button add({bool showLabel = true, VoidCallback? onPressed}) => build(
        iconData: FluentIcons.circle_addition,
        label: showLabel ? "新增" : null,
        onPressed: onPressed,
      );

  ///删除
  Button delete({bool showLabel = true, VoidCallback? onPressed}) => build(
        iconData: FluentIcons.delete,
        label: showLabel ? "删除" : null,
        onPressed: onPressed,
      );

  ///修改
  Button modify({bool showLabel = true, VoidCallback? onPressed}) => build(
        iconData: FluentIcons.edit,
        label: showLabel ? "修改" : null,
        onPressed: onPressed,
      );

  ///查询
  Button query({bool showLabel = true, VoidCallback? onPressed}) => build(
        iconData: FluentIcons.search,
        label: showLabel ? "查询" : null,
        onPressed: onPressed,
      );

  ///重置
  Button reset({bool showLabel = true, VoidCallback? onPressed}) => build(
        iconData: FluentIcons.reset,
        label: showLabel ? "重置" : null,
        onPressed: onPressed,
      );

  ///保存
  Button save({bool showLabel = true, VoidCallback? onPressed}) => build(
        iconData: FluentIcons.save,
        label: showLabel ? "保存" : null,
        onPressed: onPressed,
      );

  ///取消
  Button cancel({bool showLabel = true, VoidCallback? onPressed}) => build(
        iconData: FluentIcons.cancel,
        label: showLabel ? "取消" : null,
        onPressed: onPressed,
      );

  ///提交
  Button submit({bool showLabel = true, VoidCallback? onPressed}) => build(
        iconData: FluentIcons.chevron_up_end,
        label: showLabel ? "提交" : null,
        onPressed: onPressed,
      );

  ///查看
  Button info({bool showLabel = true, VoidCallback? onPressed}) => build(
        iconData: FluentIcons.info,
        label: showLabel ? "查看" : null,
        onPressed: onPressed,
      );
}

///按钮单例
final buttons = Buttons();
